package co.yixiang.controller.common;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import co.yixiang.common.enums.ResultCode;
import co.yixiang.common.utils.HttpClientUtils;
import co.yixiang.common.utils.JsonUtils;
import co.yixiang.common.utils.ResultUtil;
import co.yixiang.common.vo.ResultMessage;
import com.google.gson.Gson;
import org.springframework.data.redis.core.PartialUpdate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/common")
public class TianQiController {

    @GetMapping(value = "/tianqi/{location}")
    public ResultMessage<Object> getTianQi(@PathVariable String location) {

        final String s = HttpUtil.get("https://restapi.amap.com/v3/geocode/regeo?key=26b0195cd9d6b31d3cfbe9b402d19a62&location="+location);
        if (ObjectUtil.isEmpty(s)) {
           return ResultUtil.error(500,"接口请求失败!");
        }
        JSONObject body = JSONUtil.parseObj(s);
        String regeocodeJson = body.getStr("regeocode");
        JSONObject regeocode = JSONUtil.parseObj(regeocodeJson);
        String addressComponentJson = regeocode.getStr("addressComponent");
        JSONObject addressComponent = JSONUtil.parseObj(addressComponentJson);
        // 城市编码
        String adcode = addressComponent.getStr("adcode");
        final String tianQi;
        tianQi = HttpUtil.get("http://restapi.amap.com/v3/weather/weatherInfo?key=26b0195cd9d6b31d3cfbe9b402d19a62&city="+adcode);
        if (ObjectUtil.isEmpty(tianQi)) {
           return ResultUtil.error(500,"接口请求失败!");
        }
        return ResultUtil.data(JSONUtil.parseObj(tianQi));
    }
}
